using UnityEngine;

public class ControlCube : MonoBehaviour
{
	public float speed = 10.0f;
	public string name;
	private bool setGUI = true; 
	
	private Vector3 targetPos;
	float splineP = 0;
	
	DataScript data;
	
	void Start()
	{
		data = GameObject.FindGameObjectWithTag("DataManager").GetComponent<DataScript>();
		name = data.name;
		targetPos = Vector3.zero;
		networkView.RPC("Pushname",RPCMode.AllBuffered, name);
	}
		
	void Update()
	{
		
		if (!networkView.isMine)
		{
			splineP = Time.deltaTime * 10;
			transform.position = Vector3.Lerp(transform.position,targetPos,splineP);
			return;
		}
		
		
		transform.Translate(new Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, Input.GetAxis("Vertical") * speed * Time.deltaTime, 0));
		networkView.RPC("MovedTo", RPCMode.All, transform.position);
		Vector3 tmpPos = transform.position;
		tmpPos.x = Mathf.Clamp(tmpPos.x,-6,6);
		tmpPos.y = Mathf.Clamp(tmpPos.y,-5,5);
		transform.position = tmpPos;
		
	}
	
	[RPC]
	void Pushname(string message)
	{
		name = message;
	}
	[RPC]
	void MovedTo(Vector3 pos)
	{
		targetPos = pos;
		splineP = 0;
	}
}
